<?php

class Trade_refund_model extends CI_Model
{
    private $_table = 'trade_refund';

    public function total($postData)
    {
        $this->db->from($this->_table);
        if (!empty($postData['pay_id'])) {
            $this->db->where('pay_id', $postData['pay_id']);
        }
        if (!empty($postData['refund_id'])) {
            $this->db->where('refund_id', $postData['refund_id']);
        }
        if (!empty($postData['status'])) {
            $this->db->where('status', $postData['status']);
        }
        return $this->db->count_all_results();
    }

    public function page_list($postData, $page_num, $num)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        if (!empty($postData['pay_id'])) {
            $this->db->where('pay_id', $postData['pay_id']);
        }
        if (!empty($postData['refund_id'])) {
            $this->db->where('refund_id', $postData['refund_id']);
        }
        if (!empty($postData['status'])) {
            $this->db->where('status', $postData['status']);
        }
        $this->db->order_by('created_at', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    /**
     * 添加
     */
    public function insert($postData)
    {
        $data = array(
            'pay_id'         => $postData['pay_id'],
            'transaction_id' => $postData['transaction_id'],
            'refund_id'      => $postData['refund_id'],
            'total_fee'      => $postData['total_fee'],
            'refund_fee'     => $postData['refund_fee'],
            'sid'            => $postData['sid'],
            'status'         => 0, //待原路返回
            'message'        => '',
            'created_at'     => date('Y-m-d H:i:s'),
            'updated_at'     => date('Y-m-d H:i:s')
        );

        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    /**
     * 状态更新
     */
    public function updatePartById($id, $data)
    {
        $this->db->where('id', $id);
        return $this->db->update($this->_table, $data);
    }

    public function findById($id)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where('id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

    public function findByRefundId($id)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where('refund_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }
}